package com.dandelion.manager.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dandelion.commons.constants.BaseConst;
import com.dandelion.commons.web.model.AjaxResponse;
import com.dandelion.commons.web.model.DataGrid;
import com.dandelion.manager.system.domain.SysLog;
import com.dandelion.manager.system.repository.SysLogMapper;
import com.dandelion.manager.system.service.SysLogService;

@Service
public class SysLogServiceImpl implements SysLogService {
	
	@Autowired
	private SysLogMapper mapper ;

	@Override
	public AjaxResponse add(SysLog record) {
		this.mapper.insertSelective(record) ;
		return null ;
	}

	@Override
	public AjaxResponse delete(long id) {
		this.mapper.deleteByPrimaryKey(id) ;
		return null ;
	}
	
	@Override
	public AjaxResponse deleteBatch(Long[] ids) {
		this.mapper.deleteByPrimaryKeys(ids) ;
		return null ;
	}


	@Override
	public AjaxResponse update(SysLog record) {
		this.mapper.updateByPrimaryKeySelective(record) ;
		return null ;
	}

	@Override
	public SysLog get(long id) {
		return this.mapper.selectByPrimaryKey(id) ;
	}

	@Override
	public List<SysLog> getAllList(SysLog record) {
		return this.mapper.selectAllList(record);
	}
	
	@Override
	public DataGrid<SysLog> datagrid(SysLog conditions) {
		return null;
	}

	@Override
	public DataGrid<SysLog> datagridByOperateLog(SysLog dto) {
		return findLogByType(dto, BaseConst.LOG_TYPE_OPERATE) ;
	}

	@Override
	public DataGrid<SysLog> datagridByExceptionLog(SysLog dto) {
		return findLogByType(dto, BaseConst.LOG_TYPE_EXCEPTION) ;
	}
	
	private DataGrid<SysLog> findLogByType(SysLog dto, String logType) {
		DataGrid<SysLog> dg = new DataGrid<SysLog>() ;
		dto.setLogType(logType);
		
		Map<String, Object> conditionsMap = new HashMap<String, Object>() ;
		conditionsMap.put("record", dto) ;
		conditionsMap.put("offset", dg.getOffset()) ;
		conditionsMap.put("rows", dg.getPageSize()) ;
		
		List<SysLog> list = this.mapper.selectAllListPage(conditionsMap) ;
		int count = this.mapper.getCount(conditionsMap) ;
		dg.setRows(list) ;
		dg.setTotal(count);
		
		return dg;
	}
	
}
